package cn.sunshuai.controller;

import cn.sunshuai.domain.Movie;
import cn.sunshuai.domain.MovieUser;
import cn.sunshuai.exception.BusinessException;
import cn.sunshuai.service.IMovieService;
import cn.sunshuai.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class MovieController {

    @Autowired
    private IMovieService movieService;

    /**
     * 查询所有接口
     * @return
     */
    @GetMapping
    public List<Movie> findAll(){
        return movieService.findAll();
    }

    @GetMapping("/{id}")
    public Movie movieBuy(@PathVariable("id") Long id){
        return movieService.movieBuy(id);
    }

    /**
     * 接口:新增或修改
     * @param
     * @return
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody MovieUser movieUser){
        try {
            if(movieUser.getId()==null){
                movieService.add(movieUser);
            }else {
                movieService.update(movieUser);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    /**
     * 接口:新增或修改
     * @param
     * @return
     */
    @PostMapping
    public AjaxResult addOrUpdateMovie(@RequestBody Movie movie){
        try {
            if(movie.getId()==null){
                movieService.addMovie(movie);
            }else {
                movieService.updateMovie(movie);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @GetMapping("/movieUser/{id}")
    public MovieUser findByMovieId(@PathVariable("id") Long id){
        return movieService.findByMovieId(id);
    }
}
